package com.quo.controller;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quo.dto.ProductPlanDto;
import com.quo.dto.ProductsPlanDto;
import com.quo.dto.QuoDto;
import com.quo.dto.QuoInfo;
import com.quo.dto.QuotesDto;
import com.quo.entity.Emp;
import com.quo.entity.QuoDetail;
import com.quo.entity.Quote;
import com.quo.service.QuoteService;
import com.quo.service.SystemService;
import com.quo.utils.ExcelWriter;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

/** 

* @author zhoumin

*/
@RestController("quoteController")
@RequestMapping(value="/api")
public class QuoteController {
	
	
	@Autowired
	public QuoteService qService;
	
	@Autowired
	public SystemService systemService;
	
	//获取报价单列表
	  @GetMapping("/quote-all")
	  public Result getQuoteList(HttpServletRequest request){ 
		  Cookie[] cookies = request.getCookies();	
		  Integer eno = 0;
		  String pwd = null;
		  for(Cookie cookie : cookies) {
			  String name = cookie.getName();
			  String value = cookie.getValue();         	  		    
		    if(name.equals("a")) {
		    	String id = cookie.getValue(); 
		    	eno = Integer.valueOf(id);		       	
		    }		    
		    else if(name.equals("b")) {
		    	pwd = cookie.getValue();		    		    	
		    }			  	          
		  }	  		
			  Result result = new Result();
			  Emp emp = systemService.ifExists(eno,pwd);	
			  List<QuotesDto> qlist = qService.getQuoteList(); 
			  result = new Result(ResultCode.SUCCESS);
			  result.setData(qlist);   
			  return result;		    
	  }
 
	//获取所有报价单信息（个人）
	  @GetMapping("/quote-personal")
	  public Result getQuoteListForSelf(HttpServletRequest request){ 
		  Cookie[] cookies = request.getCookies();	
		  Integer eno = 0;
		  String pwd = null;
		  for(Cookie cookie : cookies) {
			  String name = cookie.getName();
			  String value = cookie.getValue();         	  		    
		    if(name.equals("a")) {
		    	String id = cookie.getValue(); 
		    	eno = Integer.valueOf(id);		       	
		    }		    
		    else if(name.equals("b")) {
		    	pwd = cookie.getValue();		    		    	
		    }			  	          
		  }	  		
		  Result result = new Result();
		  Emp emp = systemService.ifExists(eno,pwd);	
			  List<QuotesDto> qlistForSelf = qService.getQuoteListForSelf(eno);
			  result = new Result(ResultCode.SUCCESS);
			  result.setData(qlistForSelf);
			  return result;
	  }
	
	  		//获取单个详细报价单信息
		  @GetMapping("/quote-all/{qid}")
		  public Result getQuote(@PathVariable Long qid){ 
			  
			  QuoDto quoDto =	qService.getQuoDto(qid); 
			  List<QuoDetail> quoDetail = qService.getQuoDetailList(qid);
			  quoDto.setQuoDetail(quoDetail);
			  Result result=new Result(ResultCode.SUCCESS);
			  result.setData(quoDto);     
			  System.out.println(result);
			  return result;
		  
		  }
		  
			//获取单个详细报价单信息（个人）
		  @GetMapping("/quote-personal/{qid}")
		  public Result getQuoteForSelf(@PathVariable Long qid,HttpServletRequest request){ 
			  Cookie[] cookies = request.getCookies();	
			  Integer eno = 0;
			  String pwd = null;
			  for(Cookie cookie : cookies) {
				  String name = cookie.getName();
				  String value = cookie.getValue();         	  		    
			    if(name.equals("a")) {
			    	String id = cookie.getValue(); 
			    	eno = Integer.valueOf(id);		       	
			    }		    
			    else if(name.equals("b")) {
			    	pwd = cookie.getValue();		    		    	
			    }			  	          
			  }	  		
			  Emp emp = systemService.ifExists(eno,pwd);
			  QuoDto quoDto =	qService.getQuoDtoForSelf(qid,eno); 
			  List<QuoDetail> quoDetail = qService.getQuoDetailList(qid);
			  quoDto.setQuoDetail(quoDetail);
			  Result result=new Result(ResultCode.SUCCESS);
			  result.setData(quoDto);     
			  System.out.println(result);
			  return result;
		  
		  }
		  
		  //添加新报价单
		  @PostMapping("/quote")
		  public Result addQuoDto(HttpSession session,@RequestBody QuoInfo quoInfo){ 
			  Emp emp = (Emp)session.getAttribute("emp");
			  
			  quoInfo.getQuote().setCeno(emp.getEno());
			  qService.addQuoInfo(quoInfo.getQuote(),quoInfo.getQuoteDetail());      
			  
			  return new Result(ResultCode.SUCCESS);
		  
		  }
		  
		  
		//删除报价单
		  @DeleteMapping("/quote/{qid}")
		  public Result delQuoteInfo(@PathVariable(value="qid") Long qid){ 
			  qService.delQuoteInfo(qid); 
			  
			  return new Result(ResultCode.SUCCESS);
		  
		  }
		  
		//导出报价单信息
		  @PostMapping("/quote-all-export")
		  public void QuoteExport(@RequestBody Long[] qids, HttpServletRequest request, HttpServletResponse response) {
			  String[] arrays= {"番号","件名","タイプ","シリーズ","価格","在庫","コネクション","インターフェイス","ノイズリダクション","重低音","防水レベル","マイク","パッケージ内容"};
				 
				response.setContentType("application/vnd.ms-excel"); 
				response.setHeader("Content-disposition", "attachment;filename=Quote-"+System.currentTimeMillis()+".xlsx");
				List<QuotesDto> quoteList =	qService.selectByQids(qids);
		        System.out.println(quoteList);
				ExcelWriter<QuotesDto> ew = new ExcelWriter<>();
				XSSFWorkbook workbook = ew.getWorkbook(quoteList, "見積もり書一覧", QuotesDto.class,arrays);
				ServletOutputStream outputStream = null;
				try {
					outputStream=response.getOutputStream();
					workbook.write(outputStream);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					try {
						workbook.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						outputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		    }
	
		  
		//获取所有待一级审核报价单信息
	
		  @GetMapping("/quote-examine/level-one")
		  public Result getQListForFirstExamine(){ 
			  	
				  List<QuotesDto> qlist = qService.getQListForFirstExamine(); 
				  Result result=new Result(ResultCode.SUCCESS);
				  result = new Result(ResultCode.SUCCESS);
				  result.setData(qlist);   
				  return result;		    
		  }
	
		//获取单个待一级审核报价单详细信息
		  @GetMapping("/quote-examine/level-one/{qid}")
		  public Result getQuoteInfo(@PathVariable Long qid){ 
			  
			  QuoDto quoDto =	qService.getQuoDto(qid); 
			  List<QuoDetail> quoDetail = qService.getQuoDetailList(qid);
			  quoDto.setQuoDetail(quoDetail);
			  Result result=new Result(ResultCode.SUCCESS);
			  result.setData(quoDto);     
			  System.out.println(result);
			  return result;
		  
		  }
		
		//报价单批量通过
		  @PostMapping("/quote-pass/level-one")
		  public Result setAllQuotePass(@RequestBody Long[] qids){ 
		 
				  qService.setAllQuotePass(qids);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//报价单批量拒绝
		  @PostMapping("/quote-deny/level-one")
		  public Result setAllQuoteDeny(@RequestBody Long[] qids){ 
		 
				  qService.setAllQuoteDeny(qids);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//报价单单个通过
		  @PostMapping("/quote-pass/level-one/{qid}")
		  public Result setQuotePass(@PathVariable Long qid){ 
		 
				  qService.setQuotePass(qid);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//报价单单个拒绝
		  @PostMapping("/quote-deny/level-one/{qid}")
		  public Result setQuoteDeny(@PathVariable Long qid){ 
		 
				  qService.setQuoteDeny(qid);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		  
		//获取全部生产计划审核报价单信息
			
		  @GetMapping("/quote-plan")
		  public Result getProductsPlanDto(){ 
			  	
				  List<ProductsPlanDto> qlist = qService.getQListForProduce(); 
				  Result result=new Result(ResultCode.SUCCESS);
				  result = new Result(ResultCode.SUCCESS);
				  result.setData(qlist);   
				  return result;		    
		  }
	
		//获取单个生产计划审核报价单详细信息
		  @GetMapping("/quote-plan/{pid}")
		  public Result getProductPlanDto(@PathVariable Long pid){ 
			  
			  ProductPlanDto productPlanDto =	qService.getProductPlanDto(pid); 
			
			  Result result=new Result(ResultCode.SUCCESS);
			  result.setData(productPlanDto);     
			  System.out.println(result);
			  return result;
		  
		  }
		
		//生产计划审核报价单批量通过
		  @PostMapping("/quote-pass/plan")
		  public Result setAllProductPlanPass(@RequestBody Long[] qids){ 
		 
				  qService.setAllProductPlanPass(qids);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//生产计划审核报价单批量拒绝
		  @PostMapping("/quote-deny/plan")
		  public Result setAllProductPlanDeny(@RequestBody Long[] qids){ 
		 
				  qService.setAllProductPlanDeny(qids);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//生产计划审核报价单单个通过
		  @PostMapping("/quote-pass/plan/{qid}")
		  public Result setProductPlanPass(@PathVariable Long qid){ 
		 
				  qService.setProductPlanPass(qid);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		//生产计划审核报价单单个拒绝
		  @PostMapping("/quote-deny/plan/{qid}")
		  public Result setProductPlanDeny(@PathVariable Long qid){ 
		 
				  qService.setProductPlanDeny(qid);
				  		  
			  return new Result(ResultCode.SUCCESS);
		  
		  }	
		  
		  


	
}
